import { initGlobalState } from "qiankun";
import store from "@/store";

// 为vuex添加states模块,存储qiankun的全局数据
export function registerStateModule(store) {
  if (!store || !store.hasModule) return;
  if (!store.hasModule("qiankunState")) {
    const statesModule = {
      namespaced: true,
      state: {
        qiankunState: {},
      },
      actions: {
        SetQiankunnStates({ state }, data) {
          state.qiankunState = data;
        },
      },
    };
    store.registerModule("qiankunState", statesModule);
  }
}
export const qiankunState = {};
// 初始化全局下发的数据
export const qiankunActions = initGlobalState(qiankunState);
// 检测全局下发数据的改变
qiankunActions.onGlobalStateChange((state) => {
  registerStateModule(store);
  store.dispatch("qiankunState/SetQiankunnStates", state);
});
